<?php
namespace Modules\Activity\Http\Controllers\Client\Choushang;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use \Modules\Activity\Entities\Choushang\Activity;

use Validator;
use ThrowException;
use DB;

class ActivityController extends Controller
{
    public function index(Request $req)
    {
        $options = $req->all();
        $options['per_page'] = 999999;
        $options['scopes'] = ['onStock'];
        if ( isset( $options['title'] ) && $options['title'] ){
            $title = urldecode(trim($options['title']));
            $options['where'] = ['title'=>$title];
        }
        if ( isset( $options['play_type'] ) && $options['play_type'] ){
            $options['where'] = ['play_type'=>$options['play_type']];
        }
        $options['only'] = [
            'id',
            'uuid',
            'money_price',
            'score_price',
            'title',
            'thumb',
            'activity_type_text',
            'play_type',
            'activity_type'
        ];

        $list = Activity::getList($options);

        if ( isset( $options['title'] ) && $options['title'] ){
            $res = DB::select("select sum(b.stock) stocks from choushang_activity a left join choushang_skus b on a.id = b.activity_id where 
                a.title = '$title' group by activity_id having stocks =0 ");

            $res = object_to_array($res);

            $min_id = count($res) ? count($res) : 0;
            $list->min_id = $min_id;
        }

        return $list->generateListResponse();
    }

    public function show(Request $req, $uuid)
    {
        $item = Activity::getEntity([
            'uuid' => $uuid,
            'with' => ['skus', 'last_skus']
        ]);


        $only = [
            'id',
            'uuid',
            'title',
            'image_3d',
            'images',
            'money_price',
            'score_price',
            'sub_title',
            'union_status',
            'union_status_text',
            'visit_total',
            'is_invite_enabled',
            'is_share_enabled',
            'rules',
            'play_type',
            'skus' => [
                '*' => [
                    'id',
                    'uuid',
                    'title',
                    'thumb',
                    'stock',
                    'sales',
                    'odds',
                    'money_price',
                    'list_weight',
                    'score_price'
                ]
            ],
            'last_skus' => [
                '*' => [
                    'id',
                    'uuid',
                    'title',
                    'thumb',
                    'stock',
                    'sales',
                    'money_price',
                    'list_weight',
                    'score_price'
                ]
            ]
        ];

        $info = $item->getInfo($only);
        $info['stock_has'] = 0;
        $info['stock_all'] = 0;
        if ( count($info) ){
            if ( isset($info['skus']) ){
                $stock_all = array_sum(array_column($info['skus'], 'stock'));
                $info['stock_has'] = $stock_all;

                $sales_all = array_sum(array_column($info['skus'], 'sales'));
                $info['stock_all'] = $stock_all+$sales_all;

                if ( $info['play_type'] == 3 ){
                    $odds = $stock_all > 0 ? 1 / $stock_all * 100 : 0;
                    foreach ( $info['skus'] as $k => $v ){
                        $info['skus'][$k]['odds'] = $odds * $v['stock'];
                    }
                }

                $skus_arr = array_column($info['skus'], 'list_weight');
                array_multisort($skus_arr, SORT_DESC, $info['skus']);
            }
        }


        return [
            'info' => $info
        ];
    }

    public function choushangLog(Request $req, $id)
    {
        $options = $req->all();

        $limit = isset($options['limit']) && $options['limit'] > 0 ? $options['limit'] : 100;
        $offset = isset($options['offset']) && $options['offset'] > 0 ? $options['offset'] : 0;

        $db = "package_assets a 
                left join package_skus s on a.id = s.package_asset_id 
                left join users u on s.user_id = u.id 
                left join choushang_last_skus l on l.activity_id = $id and s.title = l.title ";
        $field = "a.created_at, s.title, s.thumb sku_pic, total, u.name user_name, headimg, list_weight, if(l.id>0, l.id, 0) l_id";
        $res = DB::select("select $field from $db where a.node_type = 'choushang' and a.node_id = $id order by l.id desc, list_weight desc, a.id desc limit $limit offset $offset ");

        if ( count($res) == 0 ){
            echo '';
            die;
        }

        $res = object_to_array($res);
        foreach ( $res as $k => $v ){
            $res[$k]['star'] = 0;
            $level = mb_substr(trim($v['title']), 0, 2);
            switch ($level){
                case '隐藏' : $res[$k]['star'] = 1; break;
                case '稀有' : $res[$k]['star'] = 2; break;
                case '超神' : $res[$k]['star'] = 3; break;
                case '欧皇' : $res[$k]['star'] = 4; break;
            }
        }

        return $res;
    }

    public function choushangsBang(Request $req, $id)
    {
        $options = $req->all();

        $limit = isset($options['limit']) && $options['limit'] > 0 ? $options['limit'] : 100;
        $offset = isset($options['offset']) && $options['offset'] > 0 ? $options['offset'] : 0;

        $db = "package_assets a 
                left join package_skus s on a.id = s.package_asset_id 
                left join users u on s.user_id = u.id 
                left join choushang_last_skus l on l.activity_id = $id and s.title = l.title ";
        $field = "u.name user_name, headimg, sum(if(l.id>0, 0, total)) totals";
//        $res = DB::select("select $field from $db where a.node_type = 'choushang' and a.node_id = $id order by total desc limit $limit offset $offset ");
        $res = DB::select("select $field from $db where a.node_type = 'choushang' and a.node_id = $id group by user_name, headimg having totals > 0 order by totals desc ");

        if ( count($res) == 0 ){
            echo '';
            die;
        }

        $res = object_to_array($res);
        return $res;
    }

    public function getGroupList(Request $req)
    {
        $options = $req->all();
        $status = isset($options['status']) && $options['status'] == 'off_stock' ? 0 : 1;
        $w = '';

        $header_arr = explode('-', $req->header('Client-Type'));
        $platformType = @$header_arr[0];
        $clientType = @$header_arr[2];

        if ( $platformType == 'app' && $clientType == 'app' ){
//            $w .= " and title like 'APP_%' ";
        }else{
            $w .= " and title not like 'APP_%' ";
        }

        if ( isset($options['key']) && $options['key'] ){
            $w .= " and title like '%{$options['key']}%' ";
        }
        if ( isset($options['stock_no']) && $options['stock_no'] == 0 ){
            $w .= " and stock = 0 ";
        }
        if ( isset($options['stock_no']) && $options['stock_no'] == 1 ){
            $w .= " and stock != 0 ";
        }
        if ( isset($options['play_type']) && $options['play_type'] > 0 ){
            if ( $options['play_type'] == 1 ){
                $w .= " and play_type IN (1, 3) ";
            }else{
                $w .= " and play_type = ".$options['play_type'];
            }
        }

        $db = "choushang_activity";
        $field = "title, money_price, score_price, sum(stock) stock, sum(sales) sales, count(*) cnt, max(created_at) created_at
                , min(thumb) thumb, status, max(id) id, max(uuid) uuid, max(list_weight) list_weight, max(is_invite_enabled) is_invite_enabled, max(is_share_enabled) is_share_enabled";
        $res = DB::select("select $field from $db where status = $status $w group by title, money_price, score_price, status order by list_weight desc, created_at desc ");

        if ( count($res) == 0 ){
            $res = [];
        }

        foreach ( $res as $k => $v ){
            $res[$k]->title_app = ltrim($res[$k]->title, 'APP_');
        }

        return $res;
    }


}
